package scottb.cribbage.model {

[Bindable]
public class GameStats {

    public var startDate:Date;
    public var endDate:Date;
    public var time:int;

    public var numberOfRounds:int;

    public var mostPeggingPoints:PeggingPoints;
    public var leastPeggingPoints:PeggingPoints;
    public var mostCountingPoints:CountingPoints;
    public var leastCountingPoints:CountingPoints;
    public var mostCribPoints:CribPoints;
    public var leastCribPoints:CribPoints;
    public var mostHandPoints:Points;
    public var leastHandPoints:Points;

    public function GameStats(game:Game) {

        startDate = game.startDate;
        endDate = game.endDate;
        time = game.time;

        numberOfRounds = game.round;

        for each(var team:Team in game.teams) {
            for each(var player:Player in team.players) {
                for each(var hand:Hand in player.hands) {

                    mostPeggingPoints = mostPoints(mostPeggingPoints, hand.peggingPoints) as PeggingPoints;
                    mostCountingPoints = mostPoints(mostCountingPoints, hand.countingPoints) as CountingPoints;
                    mostCribPoints = mostPoints(mostCribPoints, hand.cribPoints) as CribPoints;
                    mostHandPoints = mostPoints(mostHandPoints, hand.points);

                    leastPeggingPoints = leastPoints(leastPeggingPoints, hand.peggingPoints) as PeggingPoints;
                    leastCountingPoints = leastPoints(leastCountingPoints, hand.countingPoints) as CountingPoints;
                    leastCribPoints = leastPoints(leastCribPoints, hand.cribPoints) as CribPoints;
                    leastHandPoints = leastPoints(leastHandPoints, hand.points);
                }
            }
        }
    }
    
    private static function leastPoints(points:Points, handPoints:Points):Points {
        
        if(handPoints == null) {
            return points;
        }
        else if(!handPoints.completed) {
            return points
        }
        else if(points == null) {
            return handPoints
        }
        else if(points.points > handPoints.points) {
            return handPoints;    
        }
        else {
            return points;
        }
    }

    private static function mostPoints(points:Points, handPoints:Points):Points {

        if(handPoints == null) {
            return points;
        }
        else if(!handPoints.completed) {
            return points
        }
        else if(points == null) {
            return handPoints
        }
        else if(points.points < handPoints.points) {
            return handPoints;
        }
        else {
            return points;
        }
    }
}
}
